<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button size="small" type="primary" @click="onAdd()">添加城市</el-button>
      </div>

      <el-table
        v-loading="listLoading"
        :data="tableData"
        style="width: 100%;"
        size="small"
        row-key="id"
        :default-expand-all="false"
        ref="multipleTable"
        lazy
        :load="load"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column >
          <template slot-scope="props">
            <!-- 内层为用户列表数组 -->

            <el-table :data="props.row.children" style="width: 100%"
                      :type="props.row.children.length>0?'expand':''"
                      v-show="props.row.expand"
                      :show-header="false"
            >
              <el-table-column
                prop="name"
                min-width="150"
              />
              <el-table-column
                prop="id"
                min-width="150"

              />
              <el-table-column
                prop="create_time"
                min-width="150"
              />
            </el-table>
          </template>
        </el-table-column>

        <el-table-column

          prop="name"
          label="地区名称"
          min-width="150"
        />
        <el-table-column
          prop="id"
          label="编号"
          min-width="150"

        />
        <el-table-column
          prop="create_time"
          label="创建时间"
          min-width="150"
        />
        <el-table-column label="操作" min-width="180" fixed="right" align="centent">
          <template slot-scope="scope">
            <el-button type="text" size="small" :disabled="isChecked" @click="onAdd()">添加
            </el-button>
            <el-button type="text" size="small" @click="onEdit(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :title="cityTitle"
      :visible.sync="show"
    >
      <el-form :model="form" ref="form">
        <el-form-item
          label="父类名称"
          label-width="100px"
          prop="name"
        >
          <el-col :span="18">
            <el-input v-model="nation" disabled></el-input>
          </el-col>

        </el-form-item>

        <el-form-item
          label="名称"
          label-width="100px"
        >
          <el-col :span="18">
            <el-input v-model="form.name" placeholder="请输入名称"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDia('form')">取 消</el-button>
        <el-button type="primary" @click="sureDia('form')">确 认</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script>

import {
  merchantMenuListApi, merchantMenuCreateApi, merchantMenuUpdateApi, merchantMenuDeleteApi
} from '@/api/merchant'
import {
  cityDeleteApi,
  cityListApi, creatCityForm, updateCityForm
} from '@/api/user'

export default {
  name: 'MerchantCity',
  data() {
    return {

      expandRowKeys: [],
      cityTitle: '添加城市',
      nation: '中国',
      form: {
        name: ''
      },
      show: false,
      isChecked: false,
      listLoading: true,
      tableData: [],
      tableFrom: {
        page: 1,
        limit: 20
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {


    // 列表
    getList() {
      this.listLoading = true
      cityListApi(0).then(res => {
        this.tableData = res.data
        this.listLoading = false
        this.tableData.forEach(item => {
          item.expand = false  // 表示当前行是闭合的，不设置这个等下展开后就没法闭合了
          if(item.snum>0){
            //列表中必须要有 hasChildren 和children
            item.hasChildren = true
          }
        })
      }).catch(res => {
        this.listLoading = false
        this.$message.error(res.message)
      })
    },

    load(tree, treeNode, resolve) {
      let that = this
      setTimeout(() => {
        // 模拟网络请求
        cityListApi(tree.id).then(res => {
         let list= res.data
          list.forEach(item => {
            if(item.snum>0){
              item.hasChildren = true
            }
          })
          resolve(list);
        }).catch(res => {
          that.$message.error(res.message)
        })
      }, 1000);
    },
    pageChange(page) {
      this.tableData.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableData.limit = val
      this.getList()
    },
    // 添加
    onAdd() {
      this.$modalForm(creatCityForm()).then(() => this.getList())

    },
    cancelDia(forms) {
      this.show = false;
      this.$refs[forms].resetFields();
    },
    sureDia(forms) {
      this.show = false;
      this.$refs[forms].resetFields();
    },
    // 编辑
    onEdit(id) {
      this.$modalForm(updateCityForm(id)).then(() => this.getList())
    },
    // 删除
    handleDelete(id) {
      this.$modalSure().then(() => {
        cityDeleteApi(id).then(({message}) => {
          this.$message.success(message)
          this.getList()
        }).catch(({message}) => {
          this.$message.error(message)
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
